package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.MmochatDirectPayOrder;
import com.dragon.mmochat.model.enumType.MmochatDirectPayOrderResultType;

public class MmochatDirectPayDao extends SimpleJdbcDaoSupport {
	private static final GmRowMapper orderMapper = new GmRowMapper();

	// 创建新订单
	private static final String createNewOrderSql = "insert into direct_pay_orders(order_id,roleid,"
			+ "skyid, create_time) values(?, ?, ?, ?)";

	public void createNewOrder(long order_id, int roleid, long skyid,
			long create_time) {
		getSimpleJdbcTemplate().update(createNewOrderSql, order_id, roleid,
				skyid, create_time);
	}

	// 设置订单成功
	private static final String setOrderSuccessSql = "update direct_pay_orders set sky_chargeid = ?,"
			+ "rmb = ?, result = 1, result_info = ? "
			+ "where order_id = ? and roleid = ? and result != 1";

	public int setOrderSuccess(String sky_chargeid, int rmb,
			String result_info, long order_id, int roleId) {
		return getSimpleJdbcTemplate().update(setOrderSuccessSql, sky_chargeid,
				rmb, result_info, order_id, roleId);
	}

	// 设置订单失败
	private static final String setOrderFailedSql = "update direct_pay_orders set sky_chargeid = ?,"
			+ "rmb = ?, result = 2, result_info = ? "
			+ "where order_id = ? and roleid = ? and result = 0";

	public int setOrderFailed(String sky_chargeid, int rmb, String result_info,
			long order_id, int roleId) {
		return getSimpleJdbcTemplate().update(setOrderFailedSql, sky_chargeid,
				rmb, result_info, order_id, roleId);
	}

	// 获取当月直付总付费额
	private static final String getMonthlyDirectPayTotalValueSql = "select sum(rmb) from "
			+ "direct_pay_orders where create_time >= ? and result=1";

	public int getMonthlyDirectPayTotalValue(long lastMonthTime) {
		return getSimpleJdbcTemplate().queryForInt(
				getMonthlyDirectPayTotalValueSql, lastMonthTime);
	}

	// 查询指定玩家的充值额
	private static final String getDirectPayByIdSql = "select sum(rmb) from direct_pay_orders "
			+ "where roleid = ? and result = 1";

	public int getDirectPayById(int roleId) {
		return getSimpleJdbcTemplate().queryForInt(getDirectPayByIdSql, roleId);
	}

	// 查询指定玩家的指定时间的充值额
	private static final String getDirectPayByIdAndTimeSql = "select sum(rmb) from direct_pay_orders "
			+ "where roleid = ? and result = 1 and create_time > ? and create_time < ?";

	public int getDirectPayByIdAndTime(int roleId, long startTime, long endTime) {
		return getSimpleJdbcTemplate().queryForInt(getDirectPayByIdAndTimeSql,
				roleId, startTime, endTime);
	}

	static class GmRowMapper implements
			ParameterizedRowMapper<MmochatDirectPayOrder> {
		@Override
		public MmochatDirectPayOrder mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatDirectPayOrder order = new MmochatDirectPayOrder();
			order.setId(rs.getLong("order_id"));
			order.setRoleId(rs.getInt("roleid"));
			order.setSkyId(rs.getLong("skyid"));
			order.setSky_chargeid(rs.getString("sky_chargeid"));
			order.setResult(MmochatDirectPayOrderResultType.get(rs
					.getInt("result")));
			order.setPayTime(rs.getLong("create_time"));
			order.setRmb(rs.getInt("rmb"));
			return order;
		}
	}
}
